package com.aaa.system.service.impl;

import com.aaa.entity.Menu;
import com.aaa.entity.Role;
import com.aaa.security.util.WebUtils;
import com.aaa.system.dao.MenuDao;
import com.aaa.system.dao.RoleDao;
import com.aaa.system.service.MenuService;
import com.aaa.system.service.RoleService;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;

import java.util.List;


@Service
public class RoleServiceImpl extends ServiceImpl<RoleDao,Role> implements RoleService {
    @Autowired
    private RoleDao roleDao;

    @Autowired
    private MenuService menuService;

    @Override
    public Result selectAll(Integer currentPage, Integer pageSize, Role role) {

        PageHelper.startPage(currentPage,pageSize);
        QueryWrapper<Role> wrapper = new QueryWrapper<>();
        wrapper.like("role_name", role.getRoleName());
        List<Role> roles = roleDao.selectList(wrapper);
        PageInfo<Role> rolePage = new PageInfo<>(roles);
        return new Result(200,"查询成功",rolePage);
    }


    @Override
    public Result delete(Integer roleId) {
        int row = roleDao.deleteById(roleId);
        return new Result(200,"删除成功",row);
    }

    @Override
    public Result update(Role role) {
        int row = roleDao.updateById(role);
        return new Result(200,"编辑成功",row);
    }

    @Override
    public Result insert(Role role) {
        int row = roleDao.insert(role);
        return new Result(200,"增加成功",row);
    }

    @Override
    public Result queryMenusByRoleId(Integer roleId) {
        //查询所有菜单
        //List<Menu> menus = roleDao.queryMenusByRoleId(roleId);
        //查询所有菜单的menuId
        List<Integer> menuIds = roleDao.queryMenuIdsByRoleId(roleId);
//       List<Menu> menus1 = MenuServiceImpl.menuUtil(menus);
        return new Result(2000,"成功",menuIds);
    }

    @Override
    public Result batchInsertRoleMenu(Integer roleId, String[] menuIds) {
        Integer integer1 = roleDao.deleteByRoleId(roleId);
        Integer integer = roleDao.batchInsertRoleMenu(roleId, menuIds);
        return new Result(2000,"成功",integer);
    }
}
